package com.fkdx.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: create by yuxg
 * @date:2022/5/4
 */
public class DateUtil {

    public static String RANGE_SEPARATOR = " 至 ";


    public static final String FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String DAY_FORMAT = "yyyy-MM-dd";

    public static final String YYMMDD = "yyMMdd";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY = "yyyy";
    public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";



    //第3-8位依次为年份的后两位、2位月份、2位日期,后四位为顺序码。例如：141605041234
    public static String getYYMMDD() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYMMDD);
        try {
            return sdf.format(new Date());
        } catch (Exception e) {

        }
        return null;
    }

    //yyyyMMdd
    public static String getYYYYMMDD() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
        try {
            return sdf.format(new Date());
        } catch (Exception e) {

        }
        return null;
    }

    //yyyyMMdd
    public static String getYYYY_MM_DD() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        try {
            return sdf.format(new Date());
        } catch (Exception e) {

        }
        return null;
    }

    //yyyyMMdd
    public static String getYYYY() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY);
        try {
            return sdf.format(new Date());
        } catch (Exception e) {

        }
        return null;
    }

    //yyyyMMdd
    public static String getYYYY(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY);
        try {
            return sdf.format(d);
        } catch (Exception e) {

        }
        return null;
    }

    public static String getYYYY_MM_DD_HH_MM_SS_SSS(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_SSS);
        try {
            return sdf.format(d);
        } catch (Exception e) {

        }
        return null;
    }

    public static Date getNextDay(Date date)
    {
        //1天24小时，1小时60分钟，1分钟60秒，1秒1000毫秒
        long addTime = 1 * 24 * 60 * 60 * 1000;
        Date nextDate = new  Date(date.getTime() + addTime);
        return nextDate;
    }

    /**
     * @param strDate
     * @return {@link Date}
     */
    public static Date strToDate(String strDate) {
        if (StringUtils.isEmpty(strDate)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DAY_FORMAT);
        Date date = null;
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {

        }
        return date;
    }

    /**
     * @param date
     * @return {@link Date}
     */
    public static Date dateToDate(Date date) {
        String strDate = dateToStr(date);
        if (StringUtils.isEmpty(strDate)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DAY_FORMAT);
        Date dater = null;
        try {
            dater = sdf.parse(strDate);
        } catch (ParseException e) {

        }
        return dater;
    }

    /**
     * 将毫秒数的时间戳换算成时间对象
     *
     * @param strDate
     * @return {@link Date}
     */
    public static Date timeStrToDate(String strDate) {
        if (StringUtils.isEmpty(strDate)) {
            return null;
        }
        if (!NumberUtils.isCreatable(strDate)) {
            return null;
        }
        return new Date(Long.parseLong(strDate));
    }

    /**
     * @param date
     * @return {@link String}
     */
    public static String dateToStr(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DAY_FORMAT);
        try {
            return sdf.format(date);
        } catch (Exception e) {

        }
        return null;
    }

    public static String date2Str(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DAY_FORMAT);
        try {
            return sdf.format(date);
        } catch (Exception e) {

        }
        return null;
    }

    public static String date2Strmm(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DEFAULT);
        try {
            return sdf.format(date);
        } catch (Exception e) {

        }
        return null;
    }


    public static String date3Str(Date date,String format) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.format(date);
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 计算指定时间后推N天后的时间
     *
     * @param currentDate
     * @param delayDay
     * @return {@link Date}
     */
    public static Date calculateDelayTime(Date currentDate, int delayDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DATE, delayDay);
        return calendar.getTime();
    }

    /**
     * @param currentDate
     * @param seconds
     * @return {@link Date}
     */
    public static Date calculateDelayUsingSeconds(Date currentDate, int seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.SECOND, seconds);
        return calendar.getTime();
    }

    /**
     * @param current
     * @return {@link String}
     */
    public static String getNextDayStr(Date current) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(current);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime());
    }

    /**
     * 拆分日期
     */
    public static Pair<Date, Date> splitDate(String dateRange) {
        String[] dates = splitRange(dateRange);
        if (dates != null && dates.length > 1) {
            Date startDate = Date.from(LocalDate.parse(dates[0]).atStartOfDay().with(LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant());
            Date endDate = Date.from(LocalDate.parse(dates[1]).atStartOfDay().with(LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
            return Pair.of(startDate, endDate);
        }
        return null;
    }

    public static String[] splitRange(String timeRange) {
        return StringUtils.split(timeRange, RANGE_SEPARATOR);
    }

    public static String getDayStart() {
        return LocalDate.now() + " 00:00:00";
    }

    public static String getDayStart(Date date) {
        return date2Str(date) + " 00:00:00";
    }

    public static String getDayEnd() {
        return LocalDate.now() + " 23:59:59";
    }

    public static String getDayEnd(Date date) {
        return date2Str(date) + " 23:59:59";
    }

    public static String showTime(Date ctime) {
        String r = "";
        if (ctime == null) {
            return r;
        }

        long nowtimelong = System.currentTimeMillis();
        long ctimelong = ctime.getTime();
        long result = Math.abs(nowtimelong - ctimelong);
        if (result < 60000) {// 一分钟内
            long seconds = result / 1000;
            if (seconds == 0) {
                r = "刚刚";
            } else {
                r = seconds + "秒前";
            }
        } else if (result < 3600000) {// 一小时内
            long seconds = result / 60000;
            r = seconds + "分钟前";
        } else if (result < 86400000) {// 一天内
            long seconds = result / 3600000;
            r = seconds + "小时前";
        } else if (result < 1702967296) {// 三十天内
            long seconds = result / 86400000;
            r = seconds + "天前";
        } else {// 日期格式
            String format = "MM-dd";
            SimpleDateFormat df = new SimpleDateFormat(format);
            r = df.format(ctime);
        }
        return r;

    }

    /**
     * 两个日期之间的相差天数
     *
     * @param date
     * @param date2
     * @return
     */
    public static long dateToDateSub(Date date, Date date2) {
        // 定义项目进行天数
        long days = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            long time = sdf.parse(sdf.format(date)).getTime();
            long time1 = sdf.parse(sdf.format(date2)).getTime();
            days = ((time1 - time) / (24 * 60 * 60 * 1000));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() < endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * LocalDateTime 转 Date
     *
     * @param localDateime
     * @return
     */
    public static Date localDateTime2Date(LocalDateTime localDateime) {
        return Date.from(localDateime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 判断当前日期是否是本月最后一天
     * @param date
     * @return
     */
    public static boolean lastDayOfMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        if(calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) {
            return true;
        }else {
            return false;
        }
    }

    /*
     * 计算日期的上一个月
     */
    public static Date getPrevMonthDate(Date date,int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - n);
        return calendar.getTime();
    }

    /**
     * 获取指定时间
     * @param hour
     * @param minute
     * @param second
     * @param addDay
     * @param args
     * @return
     */
    public static Date getNeedTime(int hour, int minute, int second, int addDay, int ...args) {
        Calendar calendar = Calendar.getInstance();
        if(addDay != 0) {
            calendar.add(Calendar.DATE, addDay);
        }
        calendar.set(calendar.HOUR_OF_DAY, hour);
        calendar.set(calendar.MINUTE, minute);
        calendar.set(calendar.SECOND, second);
        if(args.length == 1) {
            calendar.set(Calendar.MILLISECOND, args[0]);
        }
        return calendar.getTime();
    }

    public static Date strToDate2(String strDate) {
        if (StringUtils.isEmpty(strDate)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DEFAULT);
        Date date = null;
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {

        }
        return date;
    }
}
